package com.kuang.conroller;

import com.kuang.pojo.Candidate;
import com.kuang.pojo.Qquestion;
import com.kuang.pojo.Uuser;
import com.kuang.service.CandidateService;
import com.kuang.service.CodeService;
import com.kuang.service.PaperService;
import com.kuang.service.UuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UuserController {
    @Autowired
    @Qualifier("UuserServiceImpl")
    private UuserService UuserService;

    //验证用户是否可以登录
    @Qualifier("CodeServiceImpl")
    @Autowired
    private CodeService CodeService;

    @Qualifier("CandidateServiceImpl")
    @Autowired
    private CandidateService CandidateService;

    @RequestMapping("/zhuce")
    public String registerU( Model model,Uuser user, Candidate Candidate){
        if(UuserService.isOne(user.getUNAME())!=0)
        {
            model.addAttribute("msg","用户名重复");
            return "zhuce";
        }
        if(CandidateService.isOneID(Candidate.getIDNUM())!=0)
        {
            model.addAttribute("msg","身份证号重复");
            return "zhuce";
        }
        if(CandidateService.isOneTEL(Candidate.getTEL())!=0)
        {
            model.addAttribute("msg","手机号重复");
            return "zhuce";
        }
//        System.out.println("zhuce=>"+user.toString());
        UuserService.addUser(user);
        Uuser uuser=new Uuser();
        uuser=UuserService.Login(user);

        Candidate.setUSERID(uuser.getUSERID());
        System.out.println(Candidate.toString());
        CandidateService.addCandidate(Candidate);
        return "shouye";
    }
    @RequestMapping("/login")
    public String login(HttpSession session,Uuser uuser, Model model){
        //用户登录
        uuser=UuserService.Login(uuser);
        model.addAttribute("USERID",uuser.getUSERID());
        session.setAttribute("USERID",uuser.getUSERID());
        session.setAttribute("RNAME",uuser.getRNAME());
        session.setAttribute("CID",CandidateService.getCByUid(uuser.getUSERID()));
        String msg="欢迎您";
        if(uuser==null){
            model.addAttribute("errorMsg","没有这个账号");
            return "shouye";
        }
        else{
            if(uuser.getLLEVEL().equals("U")){
                msg+="用户"+uuser.getRNAME();
                model.addAttribute("msg",msg);
            }
            else {
                msg+="管理员"+uuser.getRNAME();
                model.addAttribute("msg",msg);
                return "redirect:/logs";
            }
        }
        //加载报名考试信息
        List Days=CodeService.ListDay();
        List Times=CodeService.ListTime();
        List Places=CodeService.ListPlace();
        session.setAttribute("Days", Days);
        session.setAttribute("Times", Times);
        session.setAttribute("Places", Places);
        return "dating";
    }
}
